<template>
  <a-modal
    :title="this.form.id ? '修改团战' : '创建团战'"
    :visible="visible"
    :footer="null"
    :width="800"
    @cancel="hide"
  >
    <a-spin
      :spinning="loading"
    >
      <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item v-if="form.id" label="团战ID">
          <a-input v-model="form.id" disabled />
        </a-form-model-item>
        <a-form-model-item label="团战标题">
          <a-input v-model="form.title" />
        </a-form-model-item>
        <a-form-model-item label="团战描述">
          <a-textarea v-model="form.content" />
        </a-form-model-item>
        <a-form-model-item label="团战价值">
          <a-textarea v-model="form.value" />
        </a-form-model-item>
        <a-form-model-item label="到期日">
          <a-date-picker
            v-model="form.dueDate"
            type="date"
            placeholder="请选择到期日"
            style="width: 100%;"
          />
        </a-form-model-item>
        <a-form-model-item label="我的目标">
          <a-select
            v-model="form.missionIds"
            mode="multiple"
            style="width: 100%"
            placeholder="请选择你的目标"
            option-label-prop="label"
            optionFilterProp="children"
            :loading="missionLoading"
          >
            <a-select-option v-for="mission in missionOptions" :key="mission.id" :value="mission.id" :label="mission.content">
              {{ mission.content }}({{ mission.stateDesc }})
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 18, offset: 3 }" style="text-align: center">
          <a-button type="primary" @click="onSubmit">
            {{ this.form.id ? '保存' : '创建' }}
          </a-button>
          <a-button style="margin-left: 10px;" @click="hide">
            取消
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
// tool
import moment from 'moment'
import { decodeDueDate } from '@/utils/util'
// API
import { create } from '@/api/instance'
import { fetchMyMissions } from '@/api/mission'

export default {
  name: 'EditInstance',
  data () {
    return {
      visible: false, // 是否显示模态框
      loading: false, // 是否加载
      labelCol: { offset: 3, span: 4 },
      wrapperCol: { span: 14 },
      // 任务
      missionLoading: false,
      missionOptions: [],

      form: {
        id: undefined,
        title: undefined,
        value: undefined,
        content: undefined,
        dueDate: undefined,
        missionIds: []
      }
    }
  },
  methods: {
    show (record) {
      if (record) {
        console.log(record)
        this.form.id = record.id
        this.form.title = record.title
        this.form.value = record.value
        this.form.content = record.content
        this.form.dueDate = moment(decodeDueDate(record.dueDate))
      }
      this.getMissions()
      this.visible = true
    },
    hide () {
      this.visible = false
      this.reset()
    },
    reset () {
      this.form.id = undefined
      this.form.title = undefined
      this.form.value = undefined
      this.form.content = undefined
      this.form.dueDate = undefined
      this.form.missionIds = []
    },
    async getMissions () {
      this.missionLoading = true
      const content = await fetchMyMissions({
        pageNumber: 1,
        pageSize: 1000,
        isInGroup: false
      })
      this.missionOptions = content
      console.log(content)
      this.missionLoading = false
    },
    async onSubmit () {
      try {
        this.loading = true
        console.log('this.form!', this.form)
        const params = { ...this.form }
        params.missionIds = params.missionIds.join(',')
        params.dueDate = moment(this.form.dueDate).format('YYYYMMDD')
        console.log(params)
        await create(params)
        this.hide()
        this.$emit('update-list')
        this.$notification.success({
          message: '创建成功'
        })
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
